package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.behavior.dto.ReadBehaviorDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.entity.ApReadBehavior;
import com.heima.behavior.mapper.ApReadBehaviorMapper;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.behavior.service.IApReadBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.util.UserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP阅读行为表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2021-12-28
 */
@Service
public class ApReadBehaviorServiceImpl extends ServiceImpl<ApReadBehaviorMapper, ApReadBehavior> implements IApReadBehaviorService {

    @Autowired
    private IApBehaviorEntryService entryService;

    @Override
    public ResponseResult saveRead(ReadBehaviorDto dto) {
        // 获取当前登录的用户
        User user = UserThreadLocalUtil.get();
        // 获取行为实体
        ApBehaviorEntry entry = entryService.getEntry(dto.getUserId(), dto.getEquipmentId());

        // 构建阅读行为对象
        ApReadBehavior readBehavior = new ApReadBehavior();
        readBehavior.setEntryId(entry.getId());
        readBehavior.setArticleId(dto.getArticleId());
        readBehavior.setCount(1);
        readBehavior.setReadDuration(dto.getReadDuration());
        readBehavior.setPercentage(dto.getPercentage());
        readBehavior.setLoadDuration(dto.getLoadDuration());
        readBehavior.setCreatedTime(new Date());
        // 查询历史记录
        LambdaQueryWrapper<ApReadBehavior> query = new LambdaQueryWrapper<>();
        query.eq(ApReadBehavior::getArticleId, dto.getArticleId());
        query.eq(ApReadBehavior::getEntryId, entry.getId());
        ApReadBehavior one = this.getOne(query);
        if (one == null) {
            this.save(readBehavior);
        } else {
            // 将阅读的次数+1 其他的需要更新:阅读时间\阅读百分比\更新时间
            // 原始的count=1 ,同时有100个用户来更新  想要的结果是101  实际的结果可能是 2
            // 加锁synchronized 不能解决分布式下的锁的问题
            // 可以使用分布式锁 redis  redis是单线程的内存型数据库 setnx 命令  setnx(aa,val)
            // 可以使用分布式锁 zk  顺序临时节点


            // 方案一:在高并发的时候可能会有数据不一致的问题
            // one.setCount(one.getCount() + 1);
            // one.setPercentage(dto.getPercentage());
            // this.updateById(one);

            // 方案二:直接更新数量+1
            LambdaUpdateWrapper<ApReadBehavior> update = new LambdaUpdateWrapper<>();
            update.eq(ApReadBehavior::getArticleId, dto.getArticleId());
            update.eq(ApReadBehavior::getEntryId, entry.getId());
            // 设置更新条件
            // update ap_read_beha vior set count =2 where id = ?
            update.set(ApReadBehavior::getPercentage, dto.getPercentage());
            update.set(ApReadBehavior::getUpdatedTime, new Date());
            // 一条sql语句就是一个事务
            // update ap_read_beha vior set count =count+1 where id = ?
            update.setSql("count =count+1");
            this.update(update);
        }
        return ResponseResult.okResult();
    }

}
